package com.link.base.base.clue.controller;

import com.link.base.base.clue.model.Clues;
import com.link.base.base.clue.service.ClueService;
import com.link.core.cllog.LCLogger;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.query.QueryParams;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.KeyGenerateService;
import com.link.core.basic.service.ServiceException;
import com.link.core.util.JsonUtil;
import com.link.core.util.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * Copyright  hande Company. All Rights Reserved.
 *
 * @author  LiYang
 *
 * @date:   2018/11/8 16:47
 * @version 1.0
 */

@Controller
@RequestMapping("/link/clue")
public class ClueController extends BasicController<Clues> {

    @Resource
    private ClueService clueService;

    @Resource
    private KeyGenerateService keyGenerateService;

    /**
     * 获取service
     */
    @Override
    public BasicService<Clues> getBasicService() throws Exception {
        if (clueService != null) {
            return clueService;
        }
        throw new ServiceException("PUBLIC-005");
    }

    /**
     * 新建默认值
     */
    @Override
    @RequestMapping(value = "/preDefaultValue", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Map<String, Object> preDefaultValue(@JsonParam Clues entity, HttpSession session, HttpServletRequest request,
                                               HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            Clues clue = new Clues();
            Long id = keyGenerateService.keyGenerate();
            clue.setId(id);
            clue.setCode(id.toString());
            result.put("success", true);
            result.put("result", clue);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 跟进线索
     *
     * @param record
     * @param session
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/clueFollow", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> clueFollow(@JsonParam Clues record, HttpSession session, HttpServletRequest request,
                                          HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            clueService.clueFollow(record);

            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 撤回线索
     *
     * @param record
     * @param session
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/clueRecall", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> clueRecall(@JsonParam Clues record, HttpSession session, HttpServletRequest request,
                                          HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            clueService.clueRecall(record);

            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 完成线索
     *
     * @param record
     * @param session
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/clueFinish", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> clueFinish(@JsonParam Clues record, HttpSession session, HttpServletRequest request,
                                          HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            clueService.clueFinish(record);

            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 取消线索
     *
     * @param record
     * @param session
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/clueCancel", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> clueCancel(@JsonParam Clues record, HttpSession session, HttpServletRequest request,
                                          HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            clueService.clueCancel(record);

            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 线索生成客户
     *
     * @param record
     * @param session
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/clueToAccount", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> clueToAccount(@JsonParam Clues record, HttpSession session, HttpServletRequest request,
                                             HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            clueService.clueToAccount(record);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }


    @RequestMapping(value = "/formUpdate", method = RequestMethod.POST)
    @ResponseBody
    public String formUpdate(@JsonParam Clues entity, HttpSession session,
                             HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            beforUpdate(entity, request);
            beforUpsert(entity, request);
            checkData(entity, request);
            String base = "base";
            if (!checkExistDataCache(base, session, entity)) {
                throw new ServiceException("PUBLIC-002");
            }
            clueService.update(entity);
            Clues newEntry = clueService.queryById(entity);
            result.put("newRow", newEntry);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }

        String resultStr = JsonUtil.toString(result);
        return resultStr;
    }

    @Override
    protected void beforeImport(List<Clues> list, QueryParams qps, HttpServletRequest request) throws Exception {
        for (Clues c : list) {
            if (StringUtils.isNull(c.getContactName()) || StringUtils.isNull(c.getCompanyName())) {
                throw new ServiceException("CFGPROPERTY-002");
            }
        }
        super.beforeImport(list, qps, request);
    }

}
